import pymongo
client = pymongo.MongoClient('mongodb://10.8.1.35:27017/')
center_ = client["qpe-app"]
center_.authenticate("root", "gold123456", mechanism="SCRAM-SHA-1")
entity_ = center_["videoNVREntity_test"]
#仓位表
location_entity_ = center_["locationEntity_test"]
#video表
video_entity_ = center_["videoEntity_test"]
for nvrEntity in entity_.find():
    # 01查询vedioNVREntity数据库中id字段和warehouseId这两字段
    id_ = nvrEntity["_id"]
    warehouse_id_ = nvrEntity["warehouseId"]
    print("id+", id_, "warehouseId+", warehouse_id_)

    #02 循环遍历，更新 locationEntity表 根据上面warehouseId 字段更新nvrId =id
    condition = {'warehouseId': warehouse_id_}
    #根据条件查询到所有仓位    locations = location_entity_.find(condition)

    # 根据条件查询，并把匹配结果字段nvr设置
    location_entity_.update_many(condition, {'$set': {'nvrId': int(id_)}})
    #同理 更新vedioEntity表
    video_entity_.update_many(condition, {'$set': {'nvrId': int(id_)}})


client.close()
